package com.wei.czz.framework.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.wei.czz.common.enums.CommonEnum;
import com.wei.czz.common.exception.CzzException;
import com.wei.czz.common.utils.TimeUtils;
import com.wei.czz.framework.blog.dao.BlogStatsDao;
import com.wei.czz.framework.blog.entity.BlogStatsEntity;
import com.wei.czz.framework.blog.service.BlogStatsService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.*;

/**
 * Created by IntelliJ IDEA.
 *
 * @author wei
 * date: 2022-03-01 10:04:45
 * className: BlogStatsServiceImpl 博客统计操作请求服务接口实现类
 * version: 1.0
 * description:
 */
@Service("blogStatsService")
public class BlogStatsServiceImpl extends ServiceImpl<BlogStatsDao, BlogStatsEntity> implements BlogStatsService {

    @Override
    public Long getUserBlogStats(Long userId, Integer type, Integer valueType) {
        return baseMapper.getUserBlogStats(userId, type, valueType);
    }

    @Override
    public List<BlogStatsEntity> getBlogStatsList(Long blogId) {
        LambdaQueryWrapper<BlogStatsEntity> blogStatsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        blogStatsLambdaQueryWrapper.eq(Objects.nonNull(blogId), BlogStatsEntity::getBlogId, blogId);
        return baseMapper.selectList(blogStatsLambdaQueryWrapper);
    }

    @Override
    public int incrementBlogViewNum(Long blogId, Integer num) {
        // 更新博客统计的浏览次数
        int count = baseMapper.incrementBlogViewNum(blogId, num);
        if (count == 0) {
            /*
                如果更新结果为空，则说明该博客还没有统计信息，新增一条统计信息
             */
            BlogStatsEntity blogStats = new BlogStatsEntity();
            // 设置日浏览次数、月浏览次数和年浏览次数属性值
            blogStats.setBlogId(blogId)
                    .setDayViewNum(num)
                    .setMonthViewNum(num)
                    .setYearViewNum(num);
            // 操作数据库博客统计表，新增一条博客统计信息
            count = baseMapper.insert(blogStats);
            if (count == 0) {
                log.error("保存博客统计信息失败，请务必及时处理");
            }
        }
        return count;
    }

    @Override
    public int incrementBlogLikeNum(Long blogId, Integer num) {
        // 增加博客统计的点赞数
        int count = baseMapper.incrementBlogLikeNum(blogId, num);
        if (count == 0) {
            /*
                如果更新结果为空，则说明该博客还没有统计信息，新增一条统计信息
             */
            BlogStatsEntity blogStats = new BlogStatsEntity();
            // 设置日点赞数、月点赞数和年点赞数属性值
            blogStats.setBlogId(blogId)
                    .setDayLikeNum(num)
                    .setMonthLikeNum(num)
                    .setYearLikeNum(num);
            // 操作数据库博客统计表，新增一条博客统计信息
            count = baseMapper.insert(blogStats);
            if (count == 0) {
                log.error("保存博客统计信息失败，请务必及时处理");
            }
        }
        return count;
    }

    @Override
    public int decrementBlogLikeNum(Long blogId, Integer num, Date optTime) {
        // 获取系统时间对象
        Date now = new Date();
        // 获取系统时间所在日的开始时间
        Date startToDay = TimeUtils.startToDay();
        // 获取系统时间所在月的开始时间（一日零点零分零秒）
        Date startTimeOnThisMonth = TimeUtils.firstDayOnThisMonth();
        // 获取系统时间所在年的开始时间（一月一日零点零分零秒）
        Date startTimeOnThisTear = TimeUtils.firstDayOnThisYear();

        num = Math.abs(num);
        if (optTime.compareTo(startToDay) >= 0 && optTime.compareTo(now) <= 0) {
            // 减少博客统计的日点赞数
            baseMapper.decrementBlogDayLikeNum(blogId, num);
        }
        if (optTime.compareTo(startTimeOnThisMonth) >= 0 && optTime.compareTo(now) <= 0) {
            // 减少博客统计的月点赞数
            baseMapper.decrementBlogMonthLikeNum(blogId, num);
        }
        if (optTime.compareTo(startTimeOnThisTear) >= 0 && optTime.compareTo(now) <= 0) {
            // 减少博客统计的年点赞数
            baseMapper.decrementBlogYearLikeNum(blogId, num);
        }
        return CommonEnum.ONE.getValue();
    }

    @Override
    public int incrementBlogCollectNum(Long blogId, Integer num) {
        // 增加博客统计的收藏数
        int count = baseMapper.incrementBlogCollectNum(blogId, num);
        if (count == 0) {
            /*
                如果更新结果为空，则说明该博客还没有统计信息，新增一条统计信息
             */
            BlogStatsEntity blogStats = new BlogStatsEntity();
            // 设置日收藏数、月收藏数和年收藏数属性值
            blogStats.setBlogId(blogId)
                    .setDayCollectNum(num)
                    .setMonthCollectNum(num)
                    .setYearCollectNum(num);
            // 操作数据库博客统计表，新增一条博客统计信息
            count = baseMapper.insert(blogStats);
            if (count == 0) {
                log.error("保存博客统计信息失败，请务必及时处理");
            }
        }
        return count;
    }

    @Override
    public int decrementBlogCollectNum(Long blogId, Integer num, Date optTime) {
        // 获取系统时间对象
        Date now = new Date();
        // 获取系统时间所在日的开始时间
        Date startToDay = TimeUtils.startToDay();
        // 获取系统时间所在月的开始时间（一日零点零分零秒）
        Date startTimeOnThisMonth = TimeUtils.firstDayOnThisMonth();
        // 获取系统时间所在年的开始时间（一月一日零点零分零秒）
        Date startTimeOnThisTear = TimeUtils.firstDayOnThisYear();

        num = Math.abs(num);
        if (optTime.compareTo(startToDay) >= 0 && optTime.compareTo(now) <= 0) {
            // 减少博客统计的日收藏数
            baseMapper.decrementBlogDayCollectNum(blogId, num);
        }
        if (optTime.compareTo(startTimeOnThisMonth) >= 0 && optTime.compareTo(now) <= 0) {
            // 减少博客统计的月收藏数
            baseMapper.decrementBlogMonthCollectNum(blogId, num);
        }
        if (optTime.compareTo(startTimeOnThisTear) >= 0 && optTime.compareTo(now) <= 0) {
            // 减少博客统计的年收藏数
            baseMapper.decrementBlogYearCollectNum(blogId, num);
        }
        return CommonEnum.ONE.getValue();
    }

    @Override
    public int incrementBlogCommentNum(Long blogId, Integer num) {
        // 增加博客统计的评论数
        int count = baseMapper.incrementBlogCommentNum(blogId, num);
        if (count == 0) {
            /*
                如果更新结果为空，则说明该博客还没有统计信息，新增一条统计信息
             */
            BlogStatsEntity blogStats = new BlogStatsEntity();
            // 设置日评论数、月评论数和年评论数属性值
            blogStats.setBlogId(blogId)
                    .setDayCommentNum(num)
                    .setMonthCommentNum(num)
                    .setYearCommentNum(num);
            // 操作数据库博客统计表，新增一条博客统计信息
            count = baseMapper.insert(blogStats);
            if (count == 0) {
                log.error("保存博客统计信息失败，请务必及时处理");
            }
        }
        return count;
    }

    @Override
    public void resetBlogStatsOnDay() {

        LambdaUpdateWrapper<BlogStatsEntity> blogStatsLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        // 设置 日浏览次数、日点赞数、日收藏数和日评论数 字段值
        blogStatsLambdaUpdateWrapper.set(BlogStatsEntity::getDayViewNum, CommonEnum.ZERO.getValue())
                .set(BlogStatsEntity::getDayLikeNum, CommonEnum.ZERO.getValue())
                .set(BlogStatsEntity::getDayCollectNum, CommonEnum.ZERO.getValue())
                .set(BlogStatsEntity::getDayCommentNum, CommonEnum.ZERO.getValue());
        // 操作数据库博客统计，批量更新博客统计信息
        int count = baseMapper.update(null, blogStatsLambdaUpdateWrapper);
        if (count == 0) {
            throw new CzzException("批量更新博客日统计数据失败，请务必及时处理");
        }
    }

    @Override
    public void resetBlogStatsOnMonth() {
        LambdaUpdateWrapper<BlogStatsEntity> blogStatsLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        // 设置 月浏览次数、月点赞数、月收藏数和月评论数 字段值
        blogStatsLambdaUpdateWrapper.set(BlogStatsEntity::getMonthViewNum, CommonEnum.ZERO.getValue())
                .set(BlogStatsEntity::getMonthLikeNum, CommonEnum.ZERO.getValue())
                .set(BlogStatsEntity::getMonthCollectNum, CommonEnum.ZERO.getValue())
                .set(BlogStatsEntity::getMonthCommentNum, CommonEnum.ZERO.getValue());
        // 操作数据库博客统计，批量更新博客统计信息
        int count = baseMapper.update(null, blogStatsLambdaUpdateWrapper);
        if (count == 0) {
            throw new CzzException("批量修改博客月统计数据失败，请务必及时处理");
        }
    }

    @Override
    public void resetBlogStatsOnYear() {
        LambdaUpdateWrapper<BlogStatsEntity> blogStatsLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        // 设置 年浏览次数、年点赞数、年收藏数和年评论数 字段值
        blogStatsLambdaUpdateWrapper.set(BlogStatsEntity::getMonthViewNum, CommonEnum.ZERO.getValue())
                .set(BlogStatsEntity::getMonthLikeNum, CommonEnum.ZERO.getValue())
                .set(BlogStatsEntity::getMonthCollectNum, CommonEnum.ZERO.getValue())
                .set(BlogStatsEntity::getMonthCommentNum, CommonEnum.ZERO.getValue());
        // 操作数据库博客统计，批量更新博客统计信息
        int count = baseMapper.update(null, blogStatsLambdaUpdateWrapper);
        if (count == 0) {
            throw new CzzException("批量修改博客年统计数据失败，请务必及时处理");
        }
    }
}